package am.NotenProgramm.objects;


public class Grade {
	String name = "Note";
	double grade = 4.0;
	double weight = 1.0;
	boolean oral = false;
	myDate date = new myDate(0, 0, 0);

	/*
	 * If created without inputs, just add the Date from now, other Values
	 * remain default. Can be used when not all data is available
	 */
	public Grade() {
		date = new myDate();
		checkInvalidValues();
	}

	// Constructor with all details to save the grade
	public Grade(String name, double grade, double weight, boolean oral,
			myDate date) {
		this.name = name;
		this.grade = grade;
		this.weight = weight;
		this.oral = oral;
		this.date = date;
		checkInvalidValues();
	}

	// Constructor to autogenerate data from the loaded String
	public Grade(String fromString) {
		String tmpString = fromString;
		String[] splitedElements = tmpString.split("<,>");
		date = new myDate(0, 0, 0);

		// Search splitted Stings for data-tags
		for (int i = 0; i < splitedElements.length; i++) {
			if (splitedElements[i].startsWith("name=")) {
				name = splitedElements[i].substring(5);
			} else if (splitedElements[i].startsWith("grade=")) {
				grade = Double.parseDouble(splitedElements[i].substring(6));
			} else if (splitedElements[i].startsWith("weight=")) {
				weight = Double.parseDouble(splitedElements[i].substring(7));
			} else if (splitedElements[i].startsWith("day=")) {
				date.setDay(Integer.parseInt(splitedElements[i].substring(4)));
			} else if (splitedElements[i].startsWith("month=")) {
				date.setMonth(Integer.parseInt(splitedElements[i].substring(6)));
			} else if (splitedElements[i].startsWith("year=")) {
				date.setYear(Integer.parseInt(splitedElements[i].substring(5)));
			} else if (splitedElements[i].startsWith("oral=")) {
				if (splitedElements[i].substring(5).equals("true")) {
					oral = true;
				} else {
					oral = false;
				}
			}
		}
		checkInvalidValues();
	}

	void checkInvalidValues() {
		if(grade <= 1.0){
			grade = 1.0;
		} else if(grade > 6.0) {
			grade = 6.0;
		}
		if(weight < 0.0){
			weight = 0.0;
		}
	}
	
	// Generate String to Save the data
	@Override
	public String toString() {
		return "name=" + name + "<,>grade=" + grade + "<,>weight=" + weight
				+ "<,>oral=" + oral + "<,>day=" + date.getDay() + "<,>month="
				+ date.getMonth() + "<,>year=" + date.getYear() + "<grade>";
	}

	// Getters and Setters
	public double getGrade() {
		return grade;
	}

	public void setGrade(double grade) {
		this.grade = grade;
		checkInvalidValues();
	}

	public myDate getDate() {
		return date;
	}

	public void setDate(myDate date) {
		this.date = date;
		checkInvalidValues();
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
		checkInvalidValues();
	}

	public boolean isOral() {
		return oral;
	}

	public void setOral(boolean oral) {
		this.oral = oral;
		checkInvalidValues();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
		checkInvalidValues();
	}
}
